﻿using Newtonsoft.Json.Linq;
using Rocket.Core.Logging;
using System;
using System.IO;
using System.Net;
using System.Text;

namespace AroPl.Addons
{
    public enum RegisterState
    {
        Success,
        Failed,
    }

    public enum LoginState
    {
        Success,
        Banned,
        Failed
    }

    public class PluginBack
    {
        public LoginState loginState;
        public JObject result;
    }

    public class AuroraCloud
    {
        public static RegisterState RegisterUser(string Userkey)
        {
            using (WebClient web = new WebClient())
            {
                switch (JObject.Parse(web.DownloadString($"https://yunstation.work/AuroraFiles/AuroraPlugins/CreateUser.aspx?userkey={Userkey}"))["ResultCode"].ToString())
                {
                    case "0":
                        return RegisterState.Failed;
                    case "1":
                        break;
                    case "-1":
                        return RegisterState.Failed;
                }
                return RegisterState.Success;
            }
        }

        public static PluginBack LoginUser(string Userkey)
        {
            using (WebClient web = new WebClient())
            {
                JObject o = JObject.Parse(web.DownloadString($"https://yunstation.work/AuroraFiles/AuroraPlugins/PluginAuth.aspx?userkey={Userkey}"));
                switch (o["ResultCode"].ToString())
                {
                    case "0":
                        Logger.Log("[极光·未来] - AroPl - 用户密钥不存在 请重试", ConsoleColor.Yellow);
                        return new PluginBack() { loginState = LoginState.Failed };
                    case "2":
                        Logger.Log("[极光·未来] - AroPl - 用户密钥已被封禁 请联系管理员", ConsoleColor.Yellow);
                        return new PluginBack() { loginState = LoginState.Banned };
                    case "-1":
                        Logger.Log("[极光·未来] - AroPl - 初始化密钥遇到错误 请重试", ConsoleColor.Yellow);
                        return new PluginBack() { loginState = LoginState.Banned };
                    default:
                        return new PluginBack() { loginState = LoginState.Success, result = o };
                }
            }
        }

        public static void GetNoDRMFile(string Directory, string Plugin)
        {
            using (WebClient web = new WebClient())
            {
                web.DownloadFile($"https://yunstation.work/AuroraFiles/AuroraPlugins/{Plugin}.data", Directory + $"/Resources/{Plugin}.data");
            }
        }
        public static void GetDRMFile(string Directory, string Plugin, string Password)
        {
            using (WebClient web = new WebClient())
            {
                JObject jObject = new JObject();
                jObject.Add("PluginID", Plugin);
                jObject.Add("OutdateTime", DateTime.Now.AddDays(1));
                jObject.Add("DRMKey", RandomStringBuilder.Create(32));
                byte[] file = web.DownloadData($"https://yunstation.work/AuroraFiles/AuroraPlugins/{Plugin}.data");
                file = AESEncryption.AesDecryptor(file, Password);
                File.WriteAllBytes(Directory + $"/Resources/{Plugin}-drm.data", AESEncryption.AesEncryptor(file, jObject["DRMKey"].ToString()));
                File.WriteAllBytes(Directory + $"/Resources/{Plugin}.drm", AESEncryption.AesEncryptor(Encoding.UTF8.GetBytes(jObject.ToString()), FileAuth.GetMD5HashFromFile(Directory + $"/Resources/{Plugin}-drm.data")));
            }
        }
    }
}
